package com.qf.hsr.ex.resolver;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author q
 * @version V1.0
 * @date 2021/11/15
 **/
public class GlobalExceptionResolver implements HandlerExceptionResolver {

    /**
     * 核心处理异常的方式
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @return
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

        ModelAndView mav = new ModelAndView();
        mav.addObject("exMsg", ex.getMessage());
        if (ex instanceof ArithmeticException){
            mav.setViewName("errorArithmetic");
            return mav;
        }
        mav.setViewName("error");
        return mav;
    }
}
